﻿using System;
using System.Collections.Generic;

namespace SlamCms.Configuration
{
	public class ConfigurationSettingDictionary : Dictionary<string, string>
	{
		public ConfigurationSettingDictionary(Configuration configuration)
		{
			configuration["Settings.Setting"].ForEach(s => this.Add(s["Name"], s["Value"]));
		}

		public T Get<T>(string name, T defaultValue = default(T))// where T : struct
		{
			return (T)Get(typeof(T), name, defaultValue);
		}

		public object Get(Type returnType, string name, object defaultValue = null)
		{
			if (!this.ContainsKey(name))
				return defaultValue;

			var value = this[name];

			if (string.IsNullOrEmpty(value))
				return defaultValue;

			return Convert.ChangeType(value, returnType);
		}
	}
}